/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kwic;

import java.util.*;




/**
 *
 * @author Tom
 */
public class SharedMemory {

    List<String> lines;
    List<String> permutation;

    public SharedMemory() {
        lines = new ArrayList<>();
        permutation = new ArrayList<>();
    }

    private void input() {
        System.out.println("Type text, end of line finish by ENTER, end of stream finish by: . (dot)  on clear line");
        Scanner sc = new Scanner(System.in);
        String line;
        while (!(line = sc.nextLine()).equals(".")) {
            lines.add(line);
        }

    }

    private void output() {
        System.out.println("Output SEQUENCE");
        Iterator it = lines.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    private void alphabetizer() {
        
        java.util.Collections.sort(lines,String.CASE_INSENSITIVE_ORDER);
    }

    private void circuralShift() {
        Iterator itL = lines.iterator();

        while (itL.hasNext()) {
            String currLine = (String) itL.next();
            String fieldLine[] = currLine.split(" ");
            for (int j = 0; j < fieldLine.length; j++) {
                String lastWord = fieldLine[fieldLine.length - 1];
                for (int k = fieldLine.length - 1; k > 0; k--) {
                    fieldLine[k] = fieldLine[k - 1];
                }
                fieldLine[0] = lastWord;
                String complete = "";
                for (int k = 0; k < fieldLine.length; k++) {
                    complete = complete.concat(fieldLine[k]);
                    complete = complete.concat(" ");
                }            
               permutation.add(complete);
            }
        }
        lines = permutation;
    }

    public void run() {
        input();
        circuralShift();
        alphabetizer();
        output();
    }
}
